package com.xls.pet.controller;

import com.mybatisflex.core.paginate.Page;
import com.xls.pet.common.Result;
import com.xls.pet.query.WorkOrderQuery;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.xls.pet.entity.WorkOrder;
import com.xls.pet.service.WorkOrderService;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 工单 控制层。
 *
 * @author xls
 * @since 2024-07-23
 */
@RestController
@RequestMapping("/workOrder")
public class WorkOrderController {

    @Autowired
    private WorkOrderService workOrderService;

    @GetMapping("/{id}")
    public Result<WorkOrder> getWorkOrder(@PathVariable Integer id) {
        return Result.success(workOrderService.getById(id));
    }

    /**
     * 添加工单。
     *
     * @param workOrder 工单
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public Result<?> save(@RequestBody WorkOrder workOrder) {
        workOrderService.save(workOrder);
        return Result.success();
    }

    /**
     * 根据主键删除工单。
     *
     * @param ids 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{ids}")
    public Result<?> remove(@PathVariable Integer[] ids) {
        workOrderService.removeByIds(Arrays.asList(ids));
        return Result.success();
    }

    /**
     * 根据主键更新工单。
     *
     * @param workOrder 工单
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public Result<?> update(@RequestBody WorkOrder workOrder) {
        workOrderService.updateById(workOrder);
        return Result.success();
    }

    /**
     * 查询所有工单。
     *
     * @return 所有数据
     */
    @PostMapping("/list/{pageNumber}/{pageSize}/{totalRow}")
    public Result<Page<WorkOrder>> list(@PathVariable Integer pageNumber, @PathVariable Integer pageSize,
                                        @PathVariable Integer totalRow, @RequestBody WorkOrderQuery workOrderQuery) {
        return Result.success(workOrderService.list(pageNumber, pageSize, totalRow, workOrderQuery));
    }

}
